package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description NursingPlanServiceImpl
 * @Author loe
 * @Date 2024-07-08
 */

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> searchAll() {
        return nursingPlanMapper.searchAll();
    }

    /**
     * 新增
     *
     * @param nursingPlanDto
     */
    @Override
    @Transactional
    public void insert(NursingPlanDto nursingPlanDto) {
        NursingPlan nur = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nur.setStatus(1);
        nursingPlanMapper.insert(nur);

        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        if (!projectPlans.isEmpty()) {
            for (NursingProjectPlanDto projectPlan : projectPlans) {
                projectPlan.setPlanId(nur.getId());
                nursingProjectPlanMapper.insert(BeanUtil.toBean(projectPlan, NursingProjectPlan.class));
            }
        }
    }

    /**
     * 分页条件查询
     *
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> limitSearch(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.limitSearch(name, status);
        PageResponse<NursingPlanVo> pageResponse = new PageResponse<>(page);
        pageResponse.setRecords(page.getResult());
        return pageResponse;
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Integer id) {
        return nursingPlanMapper.getById(id);
    }

    /**
     * 修改护理计划及护理计划和项目关联表
     *
     * @param
     */
    @Override
    @Transactional
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        // 更新护理计划表
        nursingPlanMapper.update(nursingPlan);
        // 更新护理计划和项目关联表
        // 先删除
        Long id = nursingPlanDto.getId();
        nursingProjectPlanMapper.delete(id);
        // 再添加
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        if (!projectPlans.isEmpty()) {
            for (NursingProjectPlanDto projectPlan : projectPlans) {
                NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(projectPlan, NursingProjectPlan.class);
                nursingProjectPlan.setPlanId(id);
                nursingProjectPlanMapper.insert(nursingProjectPlan);
            }
        }
    }

    /**
     * 删除护理计划和项目关联表
     *
     * @param id 护理计划id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        // 删除护理计划表
        nursingPlanMapper.delete(id);
        // 删除护理计划和项目关联表(传入护理计划id)
        nursingProjectPlanMapper.delete(id);
    }

    /**
     * 启用禁用
     * @param id
     * @param status
     */
    @Override
    public void setStatus(Long id, Integer status) {
        nursingPlanMapper.setStatus(id, status);
    }
}
